﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class ZhenTongCountCtrlViewModel : BaseViewModel<ZhenTongCountCtrlViewModel>
    {
        #region Variables
        private int _startAge;

        private int _endAge = 10;

        //开始时间
        protected DateTime _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
        //结束时间
        protected DateTime _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
        //右边用于显示的具体麻醉医生的所有病人信息
        private ObservableCollection<StatisticsPatientLinkModel> _disPatientLinkModelList = new ObservableCollection<StatisticsPatientLinkModel>();
        //条件查询到的所有病人信息
        private List<PatientLinkModel> _patientLinkModels = new List<PatientLinkModel>();

        private ObservableCollection<string> zhenTongMethodList = new ObservableCollection<string>();

        private string _selectedZhenTongMethod = "";
        private ObservableCollection<MonthModel> monthMethodList = new ObservableCollection<MonthModel>();
        private MonthModel _selectedMonthMethod;
        #endregion

        #region Constructor

        public ZhenTongCountCtrlViewModel()
        {
            initZhenTongMethod();
            CreateMonthMondelList();
        }
        #endregion

        #region Properties
        public ObservableCollection<StatisticsPatientLinkModel> DisPatientLinkModelList
        {
            get
            {
                return _disPatientLinkModelList;
            }

            set
            {
                _disPatientLinkModelList = value;
                RaisePropertyChanged("DisPatientLinkModelList");
            }
        }

        public int StartAge
        {
            get
            {
                return _startAge;
            }

            set
            {
                _startAge = value;
            }
        }

        public int EndAge
        {
            get
            {
                return _endAge;
            }

            set
            {
                _endAge = value;
            }
        }

        public DateTime StartDate
        {
            get
            {
                return _startDate;
            }

            set
            {
                _startDate = value;
                RaisePropertyChanged("StartDate");
            }
        }

        public DateTime EndDate
        {
            get
            {
                return _endDate;
            }

            set
            {
                _endDate = value;
                RaisePropertyChanged("EndDate");
            }
        }

        public List<PatientLinkModel> PatientLinkModels
        {
            get
            {
                return _patientLinkModels;
            }

            set
            {
                _patientLinkModels = value;
            }
        }

        public ObservableCollection<string> ZhenTongMethodList
        {
            get
            {
                return zhenTongMethodList;
            }

            set
            {
                zhenTongMethodList = value;
                //RaisePropertyChanged("ZhenTongMethodList");
            }
        }

        public string SelectedZhenTongMethod
        {
            get
            {
                return _selectedZhenTongMethod;
            }

            set
            {
                _selectedZhenTongMethod = value;
                RaisePropertyChanged("SelectedZhenTongMethod");
            }
        }

        public ObservableCollection<MonthModel> MonthMethodList
        {
            get
            {
                return monthMethodList;
            }

            set
            {
                monthMethodList = value;
            }
        }

        public MonthModel SelectedMonthMethod
        {
            get
            {
                return _selectedMonthMethod;
            }

            set
            {
                _selectedMonthMethod = value;
                GetMonthStartDate();
                RaisePropertyChanged("SelectedMonthMethod");
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void CreateMonthMondelList()
        {
            MonthMethodList.Add(new MonthModel("", 0));
            string[] strArr = { "一月", "二月", "三月", "四月", "五月", "半年", "七月", "八月", "九月", "十月", "十一月", "一年" };
            for (int i = 0; i < strArr.Length; i++)
            {
                MonthMethodList.Add(new MonthModel("近" + strArr[i], i + 1));
            }
        }
        private void GetMonthStartDate()
        {

            EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            if (DateTime.Now.Month - SelectedMonthMethod.Code < 0)
            {
                StartDate = new DateTime(DateTime.Now.Year - 1, 13 + (DateTime.Now.Month - SelectedMonthMethod.Code), 1, 0, 0, 0);
            }
            else
            {
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - SelectedMonthMethod.Code + 1, 1, 0, 0, 0);
            }
        }
        #endregion

        #region Public/Protected Methods
        public void Excel()
        {

            if (PatientLinkModels == null || PatientLinkModels.ToList().Count == 0)
            {
                MessageBoxCommon.ShowDialog("请先查询病人!", MessageBoxCommon.OK);
                return;
            }
            ExportToExcel excel = new ExportToExcel();
            excel.ExportParameterCommon("历史手术", PatientLinkModels.ToList());
        }
        public void Seach()
        {
            PatientLinkModels.Clear();
            _disPatientLinkModelList.Clear();
            //IEnumerable<PatientLinkModel> PatientLinkModels = PatientLinkService.Instance.GetBaseCount(StartDate, EndDate).OrderByDescending(O => O.Status.InRoomTime)
            //    .Where(O => O.Patient.NumAge <= EndAge && O.Patient.NumAge > StartAge).ToList();
            IEnumerable<PatientLinkModel> PatientLinks = PatientLinkService.Instance.GetBaseCount(StartDate, EndDate).OrderByDescending(O => O.Status.InRoomTime).ToList();

            if (SelectedZhenTongMethod.Equals(""))
            {
                foreach (var item in PatientLinks)
                {
                    PatientLinkModels.Add(item);
                    _disPatientLinkModelList.Add(item.ConversionPatientLink());
                }
            }
            else if (SelectedZhenTongMethod.Equals("未用镇痛泵"))
            {
                foreach (var item in PatientLinks)
                {
                    if (!CheckPCA(item))
                    {
                        PatientLinkModels.Add(item);
                        _disPatientLinkModelList.Add(item.ConversionPatientLink());
                    }
                }
            }
            else
            {
                foreach (var item in PatientLinks)
                {
                    if (CheckSelPCA(item))
                    {
                        PatientLinkModels.Add(item);
                        _disPatientLinkModelList.Add(item.ConversionPatientLink());
                    }
                }
            }
        }

        public void initZhenTongMethod()
        {
            zhenTongMethodList.Add("");
            zhenTongMethodList.Add("PCIA");
            zhenTongMethodList.Add("PCEA");
            zhenTongMethodList.Add("PCNA");
            zhenTongMethodList.Add("未用PCA");
            zhenTongMethodList.Add("未用镇痛泵");

        }

        public bool CheckPCA(PatientLinkModel p)
        {
            bool flag = false;
            if (p.AnesthesiaRecord != null &&
                (p.AnesthesiaRecord.PCEA || p.AnesthesiaRecord.PCIA || p.AnesthesiaRecord.PCNA || p.AnesthesiaRecord.WyPCA))
            {
                flag = true;
            }
            return flag;
        }

        public bool CheckSelPCA(PatientLinkModel p)
        {
            bool flag = false;
            if (p.AnesthesiaRecord != null)
            {
                if (SelectedZhenTongMethod.Equals("PCIA"))
                {
                    if (p.AnesthesiaRecord.PCIA && ScreenZtpf(p)) flag = true;
                }
                else if (SelectedZhenTongMethod.Equals("PCEA"))
                {
                    if (p.AnesthesiaRecord.PCEA && ScreenZtpf(p)) flag = true;
                }
                else if (SelectedZhenTongMethod.Equals("PCNA"))
                {
                    if (p.AnesthesiaRecord.PCNA && ScreenZtpf(p)) flag = true;
                }
                else if (SelectedZhenTongMethod.Equals("未用PCA"))
                {
                    if (p.AnesthesiaRecord.WyPCA && ScreenZtpf(p)) flag = true;
                }
            }
            return flag;
        }


        public bool ScreenZtpf(PatientLinkModel p)
        {
            bool flag = false;
            List<string> ztpfs = new List<string>();
            ztpfs = DataManager.Instance.GetXMLTableValue(p, "ShuHouZhenTong", new string[] { "sj3", "ym3", "jl3", "jytj3", "yz3", "hdz3", "zxz3" });
            double ztpf = 0;
            if (ztpfs.Count == 7)
            {
                foreach (var item in ztpfs)
                {
                    if (ztpf < item.ToDouble())
                    {
                        ztpf = item.ToDouble();
                    }
                }
            }
            if (ztpf >= StartAge && ztpf <= EndAge)
            {
                flag = true;
            }
            return flag;
        }
        #endregion

        #region EventHandlers

        #endregion
    }
}
